﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.RouteDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;

namespace WcsTestProject
{
    public class RouteGI1Init : ISettingInit
    {
        private IServiceProvider _provider;
        private ISnowflake snowflake;
        private IRouteInfoRepository routeInfoRepository;
        private IRouteDetailInfoRepository routeDetailInfoRepository;

        public RouteGI1Init(IServiceProvider provider)
        {
            _provider = provider;
            snowflake = _provider.GetService<ISnowflake>();
            routeInfoRepository = provider.GetService<IRouteInfoRepository>();
            routeDetailInfoRepository = provider.GetService<IRouteDetailInfoRepository>();
        }

        public async Task<bool> Init()
        {
            await InitS201_A014();
            await InitS201_A015();
            await InitA014_A013();
            await InitA015_A013();
            await InitA013_S211();

            return true;
        }

        //Route S201-A014
        protected async Task<bool> InitS201_A014()
        {
            RouteInfo routeInfo = new RouteInfo();
            routeInfo.ID = snowflake.GetId();
            routeInfo.FromStation = "S201";
            routeInfo.ToStation = "A014";
            await routeInfoRepository.InsertAsync(routeInfo);

            List<RouteDetailInfo> list = new List<RouteDetailInfo>();
            var rd1 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = 0,
                RouteID = routeInfo.ID,
                IsStart = true,
                IsEnd = false,
                DispatchType = EnumDispatchType.Planar,
                FromEquipment = "FromEquipment",
                FromCargo = "",
                FromCargoColumn = "0",
                ToEquipment = "G1213",
                ToCargo = "F02710000101"
            };
            list.Add(rd1);

            var rd2 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd1.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = true,
                DispatchType = EnumDispatchType.SC_UP,
                FromEquipment = "G1213",
                FromCargo = "F02710000101",
                FromCargoColumn = "0",
                ToEquipment = "SC014",
                ToCargo = "ToCargo"
            };
            list.Add(rd2);

            await routeDetailInfoRepository.InsertRangeAsync(list);

            return true;
        }

        //Route S201-A015
        protected async Task<bool> InitS201_A015()
        {
            RouteInfo routeInfo = new RouteInfo();
            routeInfo.ID = snowflake.GetId();
            routeInfo.FromStation = "S201";
            routeInfo.ToStation = "A015";
            await routeInfoRepository.InsertAsync(routeInfo);

            List<RouteDetailInfo> list = new List<RouteDetailInfo>();
            var rd1 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = 0,
                RouteID = routeInfo.ID,
                IsStart = true,
                IsEnd = false,
                DispatchType = EnumDispatchType.Planar,
                FromEquipment = "FromEquipment",
                FromCargo = "",
                FromCargoColumn = "0",
                ToEquipment = "G1209",
                ToCargo = "F02910000101"
            };
            list.Add(rd1);

            var rd2 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd1.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = true,
                DispatchType = EnumDispatchType.SC_UP,
                FromEquipment = "G1209",
                FromCargo = "F02910000101",
                FromCargoColumn = "0",
                ToEquipment = "SC015",
                ToCargo = "ToCargo"
            };
            list.Add(rd2);

            await routeDetailInfoRepository.InsertRangeAsync(list);

            return true;
        }

        //Route A014-A013
        protected async Task<bool> InitA014_A013()
        {
            RouteInfo routeInfo = new RouteInfo();
            routeInfo.ID = snowflake.GetId();
            routeInfo.FromStation = "A014";
            routeInfo.ToStation = "A013";
            await routeInfoRepository.InsertAsync(routeInfo);

            List<RouteDetailInfo> list = new List<RouteDetailInfo>();
            var rd1 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = 0,
                RouteID = routeInfo.ID,
                IsStart = true,
                IsEnd = false,
                DispatchType = EnumDispatchType.SC_DOWN,
                FromEquipment = "SC014",
                FromCargo = "FromCargo",
                FromCargoColumn = "0",
                ToEquipment = "G1307",
                ToCargo = "F02710000201"
            };
            list.Add(rd1);

            var rd2 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd1.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = false,
                DispatchType = EnumDispatchType.Planar,
                FromEquipment = "G1307",
                FromCargo = "",
                FromCargoColumn = "0",
                ToEquipment = "G1311",
                ToCargo = "E02510000201"
            };
            list.Add(rd2);

            var rd3 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd2.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = true,
                DispatchType = EnumDispatchType.SC_UP,
                FromEquipment = "G1311",
                FromCargo = "E02510000201",
                FromCargoColumn = "0",
                ToEquipment = "SC013",
                ToCargo = "ToCargo"
            };
            list.Add(rd3);

            await routeDetailInfoRepository.InsertRangeAsync(list);

            return true;
        }

        //Route A015-A013
        protected async Task<bool> InitA015_A013()
        {
            RouteInfo routeInfo = new RouteInfo();
            routeInfo.ID = snowflake.GetId();
            routeInfo.FromStation = "A015";
            routeInfo.ToStation = "A013";
            await routeInfoRepository.InsertAsync(routeInfo);

            List<RouteDetailInfo> list = new List<RouteDetailInfo>();
            var rd1 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = 0,
                RouteID = routeInfo.ID,
                IsStart = true,
                IsEnd = false,
                DispatchType = EnumDispatchType.SC_DOWN,
                FromEquipment = "SC015",
                FromCargo = "FromCargo",
                FromCargoColumn = "0",
                ToEquipment = "G1301",
                ToCargo = "F02910000201"
            };
            list.Add(rd1);

            var rd2 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd1.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = false,
                DispatchType = EnumDispatchType.Planar,
                FromEquipment = "G1301",
                FromCargo = "",
                FromCargoColumn = "0",
                ToEquipment = "G1311",
                ToCargo = "E02510000201"
            };
            list.Add(rd2);

            var rd3 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd2.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = true,
                DispatchType = EnumDispatchType.SC_UP,
                FromEquipment = "G1311",
                FromCargo = "E02510000201",
                FromCargoColumn = "0",
                ToEquipment = "SC013",
                ToCargo = "ToCargo"
            };
            list.Add(rd3);

            await routeDetailInfoRepository.InsertRangeAsync(list);

            return true;
        }

        //Route A013-S211
        protected async Task<bool> InitA013_S211()
        {
            RouteInfo routeInfo = new RouteInfo();
            routeInfo.ID = snowflake.GetId();
            routeInfo.FromStation = "A013";
            routeInfo.ToStation = "S211";
            await routeInfoRepository.InsertAsync(routeInfo);

            List<RouteDetailInfo> list = new List<RouteDetailInfo>();
            var rd1 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = 0,
                RouteID = routeInfo.ID,
                IsStart = true,
                IsEnd = false,
                DispatchType = EnumDispatchType.SC_DOWN,
                FromEquipment = "SC013",
                FromCargo = "FromCargo",
                FromCargoColumn = "0",
                ToEquipment = "I2108",
                ToCargo = "E02510100101"
            };
            list.Add(rd1);

            var rd2 = new RouteDetailInfo()
            {
                ID = snowflake.GetId(),
                PrevionsID = rd1.ID,
                RouteID = routeInfo.ID,
                IsStart = false,
                IsEnd = true,
                DispatchType = EnumDispatchType.Planar,
                FromEquipment = "I2108",
                FromCargo = "E02510100101",
                FromCargoColumn = "0",
                ToEquipment = "I1106",
                ToCargo = ""
            };
            list.Add(rd2);

            await routeDetailInfoRepository.InsertRangeAsync(list);

            return true;
        }
    }
}
